package com.pocketmenu.business;

//import org.openintents.sensorsimulator.hardware.Sensor;
//import org.openintents.sensorsimulator.hardware.SensorEvent;
//import org.openintents.sensorsimulator.hardware.SensorEventListener;
//import org.openintents.sensorsimulator.hardware.SensorManagerSimulator;


import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.util.Log;

public abstract class SensorTestActivity implements SensorEventListener {

	
	 private long lastUpdate;
//	 private SensorManagerSimulator sensorManager;
	 private SensorManager sensorManager;
	 public SensorTestActivity(SensorManager sensorx){
//		 sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
		 sensorManager=sensorx;
		    lastUpdate = System.currentTimeMillis();
	 }
	 
	 @Override
	public void onAccuracyChanged(Sensor arg0, int arg1) {
		// TODO Auto-generated method stub
		
	}
	 
	 @Override
	public void onSensorChanged(SensorEvent event) {
//		Log.i("shake", "evento");
		 if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
//		if (event.type == Sensor.TYPE_ACCELEROMETER) {
//			 Log.i("shake", "es acelerometro");
		      getAccelerometer(event);
		 }

	}
	
	 private void getAccelerometer(SensorEvent event) {
		    float[] values = event.values;
		    // Movement
		    float x = values[0];
		    float y = values[1];
		    float z = values[2];

		    float accelationSquareRootx = (x * x)
		        / (SensorManager.GRAVITY_EARTH * SensorManager.GRAVITY_EARTH);
		    float accelationSquareRooty = (y * y)
			        / (SensorManager.GRAVITY_EARTH * SensorManager.GRAVITY_EARTH);
		    float accelationSquareRootz = (z * z)
			        / (SensorManager.GRAVITY_EARTH * SensorManager.GRAVITY_EARTH);
			    
		    long actualTime = System.currentTimeMillis();

		    
//		    Log.i("shake", "vamos: "+accelationSquareRoot);
		    if (accelationSquareRootx >= 4) //

		    {
		      if (actualTime - lastUpdate < 2000) {
		        return;
		      }
		      lastUpdate = actualTime;
		      onShakex();
		    }
		    else if (accelationSquareRooty >= 4) //
		    {
//		    	Log.i("shake", "paso el if");
		      if (actualTime - lastUpdate < 2000) {
		        return;
		      }
		      lastUpdate = actualTime;
//		      Toast.makeText(this, "Device was shuffed", Toast.LENGTH_SHORT)
//		          .show();
		      //aqui poner q hacer cuando lo muevan con una funcion abstract
		      onShakey();
		    }
		    else if (accelationSquareRootz >= 4) //
		    {
//		    	Log.i("shake", "paso el if");
		      if (actualTime - lastUpdate < 2000) {
		        return;
		      }
		      lastUpdate = actualTime;
//		      Toast.makeText(this, "Device was shuffed", Toast.LENGTH_SHORT)
//		          .show();
		      //aqui poner q hacer cuando lo muevan con una funcion abstract
		      onShakez();
		    }
		  }
		


		public abstract void onShakex();
		public abstract void onShakey();
		public abstract void onShakez();

}
